package boun.center;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class BounMapActivity extends MapActivity {
    private MapView mapView;
    private BounLocationsOverlay bounLocationsOverlay;
    
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		mapView = (MapView) findViewById(R.id.bounmap);
		mapView.setBuiltInZoomControls(true);
		
		bounLocationsOverlay = createBounLocationsOverlay();
		
		showOverlay();
		
		final MapController mapController = mapView.getController();
		LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		LocationListener locationListener = new LocationListener() {
            
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
            }
            
            @Override
            public void onLocationChanged(Location location) {
                int lat = (int) (location.getLatitude() * 1E6);
                int lng = (int) (location.getLongitude() * 1E6);
                GeoPoint point = new GeoPoint(lat, lng);
                mapController.setCenter(point);
                mapController.animateTo(point);
            }
        };
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        mapController.animateTo(bounLocationsOverlay.getCenter());
        mapController.setZoom(20);
//        mapController.setZoom(15);
        
	}
	
	private void showOverlay() {
        List<Overlay> mapOverlays = mapView.getOverlays();
        mapOverlays.add(bounLocationsOverlay);
        mapView.invalidate();
	}
	
	private BounLocationsOverlay createBounLocationsOverlay() {
	    Drawable drawable = this.getResources().getDrawable(R.drawable.normal_pin);

	    BounLocationsOverlay bounLocationsOverlay = new BounLocationsOverlay(drawable,
	            mapView.getContext());

	    for (MapLocation location : getLocations()) {
	        OverlayItem overlayitem = new OverlayItem(location.getGeoPoint(), location.getTitle(),
	                location.getDescription());
	        bounLocationsOverlay.addOverlay(overlayitem);
	    }
	    return bounLocationsOverlay;
	}
	
	private ArrayList<MapLocation> getLocations() {
	    ArrayList<MapLocation> locationList = new ArrayList<MapLocation>();
	    
	    locationList.add(new MapLocation("Library", "Aptullah Kuran Library", 41.085485, 29.044249));
	    locationList.add(new MapLocation("ETA2", "Computer Engineering Department", 41.086028, 29.043991));
	    locationList.add(new MapLocation("KB", "Kare Blok (Electric & Electronics, Physics, Chemistry Departments)", 41.085849, 29.044793));
	    locationList.add(new MapLocation("New Hall", "Canteen, Big Lecture Rooms", 41.086459, 29.044933));
	    locationList.add(new MapLocation("Kuzey Park", "Molecular Biology & Genetics", 41.087959, 29.043664));
	    
	    return locationList;
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}
